Skip to content

Don't report rejected pipe steps as unhandled rejections - #8835

Closed
danlapid wants to merge 2 commits into
mainfrom
dlapid/pipe-js-source-unhandled-rejection
Closed

danlapid wants to merge 2 commits into
mainfrom
dlapid/pipe-js-source-unhandled-rejection

Conversation

@danlapid

@danlapid danlapid commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

When a JavaScript-backed ReadableStream errors while piped to a WritableStream, workerd reports an unhandled rejection even though the caller handles the pipeTo() promise and every other promise it can see:

function erroring() {
  let sent = false;
  return new ReadableStream({ type: 'bytes', pull(c) {
    if (sent) { c.error(new Error('source failed')); return; }
    sent = true; c.enqueue(new Uint8Array(1024));
  }});
}
const d = new crypto.DigestStream('MD5'); d.digest.catch(() => {});
await erroring().pipeTo(d).catch(() => {}); // unhandledrejection fires

It reproduces with JavaScript-backed writables (DigestStream, new WritableStream(), tee() branches piped to them) and with internal ones (IdentityTransformStream). It also reproduces with an internal source, such as an IdentityTransformStream readable piped into a DigestStream whose writable side is aborted, with or without preventAbort. The noise makes workerd test runs fail and clutters logs.

Cause

Both pipe loops (WritableStreamJsController::pipeLoop in standard.c++, WritableStreamInternalController::Pipe::pipeLoop in internal.c++) return promises from promise continuations that are already rejected. V8 attaches the handler that adopts such a promise only in a later microtask (the promise-resolve-thenable job). Without unhandled_rejection_after_microtask_checkpoint, unhandled rejections are processed in a microtask, which runs before that job and reports the promise. The internal readable's read-failure continuation does the same, and the pipe reaches it when aborting an internal writable whose readable side is being read.

Fix

These promises are only steps of the pipe: the caller learns the outcome from the pipe promise, which is settled on its own (and is still reported if the caller doesn't handle it). Add a rejectedPipeStep() helper that returns them marked as handled, and use it at every such site. This replaces the JavaScript-backed loop's marking that applied only to pipeThrough(), so IsPipeThrough and the corresponding parameter of rejectAfterCancel() are removed. The read-failure continuation marks its promise handled too.

Testing

  • New pipe-js-source-error-unhandled-rejection-test covers a byte source and a default source piped to DigestStream, a byte source piped to IdentityTransformStream, a teed source with both branches piped to DigestStreams, and an aborted IdentityTransformStream readable piped into a DigestStream, with and without preventAbort. It pins no_unhandled_rejection_after_microtask_checkpoint, the behavior under which these were reported. All six cases fail without the fix and pass with it, 5/5 runs.
  • Existing streams, crypto, pipe, node streams and WPT streams tests pass, including the new test's @all-autogates and @all-compat-flags variants.

🤖 Generated with Claude Code

danlapid and others added 2 commits November 23, 2026 14:07
When a JavaScript-backed ReadableStream errors while it is piped to a
WritableStream, the pipe reported an unhandled rejection even though
the caller handled the pipeTo() promise and every other promise it
could see. This happens with JavaScript-backed writables (including
crypto.DigestStream and tee() branches piped to them) and with
internal ones such as IdentityTransformStream.

Both pipe loops return promises that are already rejected from
promise continuations. V8 attaches the handler that adopts such a
promise only in a later microtask. When unhandled rejections are
processed in a microtask, which is the behavior without the
unhandled_rejection_after_microtask_checkpoint compatibility flag,
that processing can run first and report the promise.

These promises are only steps of the pipe: the caller learns the
outcome from the pipe promise, which is settled on its own. Mark them
as handled through a new rejectedPipeStep() helper. This replaces the
pipeThrough-only marking in the JavaScript-backed pipe loop, so the
IsPipeThrough parameter of rejectAfterCancel() goes away. The
internal readable's read-failure continuation returns a rejected
promise the same way, and is reached when the pipe aborts an internal
writable whose readable side is being read; mark that one handled too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add regression cases for an IdentityTransformStream readable that is
piped into a DigestStream and errors because its writable side is
aborted, with and without preventAbort. Each used to report two
unhandled rejections: one from the internal readable's read-failure
continuation and one from the JavaScript-backed pipe loop. The
previous commit fixes both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@danlapid

Copy link
Copy Markdown
Collaborator Author

Closing: this change continues downstream.

@danlapid danlapid closed this Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant